// DeviceInfo.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "windev.h"
//#include "AstroSevAPITest.h"
#include <Pkfuncs.h>
#include <Winbase.h>
#include <sipapi.h>
#include <pWinuser.h>
#define atow(strA,strW,lenW) MultiByteToWideChar(CP_ACP,0,strA,-1,strW,lenW)
#define wtoa(strW,strA,lenA) WideCharToMultiByte(CP_ACP,0,strW,-1,strA,lenA,NULL,NULL)


int GetPlatFormVersion()
{
	DWORD  data[8] ={0};

	if (!SystemParametersInfo( SPI_GETPLATFORMVERSION, sizeof(DWORD)*8,  (PVOID)& data[0] , 0))
	{
		DWORD dwerr = GetLastError();
		RETAILMSG (1, (TEXT("GetPlatFormVersion::SystemParametersInfo  Platform Version ERROR = %d \r\n"),dwerr));
	}else
	{
	
		return (data[0]);
	}
   return 0 ;
}


void GetSystemInfo()
{
	
	DWORD dwByteTransfered;
	//Device Id
	CHAR* Str;
	DEVICE_ID DeviceID;
	PDEVICE_ID pDeviceID; 
	LPWSTR pPresetIDOffset ;
	DWORD	dwPresetIDBytes;
	LPWSTR   pPlatformIDOffset;
	TCHAR   tPlatformIDOffset[100];
	TCHAR   szTmp[512] = {0};
	DWORD	dwPlatformIDBytes;
	//UUID 
	//CString strTmp;
	WCHAR UUIDBuffer[13]={0};//add terminate flag	
	TCHAR listBuffer[100];
	
	DeviceID.dwSize = 0;
	RETAILMSG (1, (TEXT("----------------GetSystemInfo ----------------\r\n")));
	RETAILMSG (1, (TEXT("Query DEVICEINFO SIZE...\r\n")));
	if (!KernelIoControl(IOCTL_HAL_GET_DEVICEID, 
		NULL, 
		0,
		(LPVOID)&DeviceID,
		sizeof(DEVICE_ID),
		&dwByteTransfered) && (GetLastError() ==ERROR_INSUFFICIENT_BUFFER) )
	{
		RETAILMSG (1, (TEXT("DEVICE INFO NEED = %d \r\n"),DeviceID.dwSize));
	}
	
	Str = (CHAR *)malloc(DeviceID.dwSize);
	
	memset(listBuffer,0,100*sizeof(WCHAR));
	KernelIoControl(IOCTL_HAL_GET_DEVICEID, 
		NULL, 
		0,
		(LPVOID)Str,
		DeviceID.dwSize,
		&dwByteTransfered);
	pDeviceID = (PDEVICE_ID)Str;
	pPresetIDOffset = (LPWSTR)(Str+pDeviceID->dwPresetIDOffset);
	dwPresetIDBytes = pDeviceID->dwPresetIDBytes;
	
	pPlatformIDOffset = (LPWSTR)(Str + pDeviceID->dwPlatformIDOffset);

	dwPlatformIDBytes = pDeviceID->dwPlatformIDBytes;

	wcscat(szTmp,L"\r\nPresetID:");
	wcscat(szTmp,pPresetIDOffset);
	wcscat(szTmp,L"\r\n\r\n");
	TCHAR data[64] = {0};
	BYTE bt;
	wcscat(szTmp,L"Platform ID (16 Bytes):\r\n");
	{
		int i=0;
		LPBYTE lpbtme = (LPBYTE )pPlatformIDOffset;
		RETAILMSG (1, (TEXT("Device ID Info:\r\n")));
		for (i = 0; i < dwPlatformIDBytes ;i++)
		{
			bt = *lpbtme++;
			RETAILMSG (1, (TEXT(" 0x%02x"),bt));
			wsprintf(data,L"%02x",bt);
			if (i % 16 ==15)
				RETAILMSG (1, (TEXT(" \r\n")));
			wcscat(szTmp,data);
		}

		RETAILMSG (1, (TEXT("\r\nEnd of Device ID Info***********!\r\n\r\n")));
	}
	wcscat(szTmp,L"\r\n\r\n");

	RETAILMSG (1,( szTmp));
	RETAILMSG (1, (TEXT("Get Device ID dwSize = %d \r\n"),pDeviceID->dwSize));
	RETAILMSG (1, (TEXT("Get Device ID dwPresetIDOffset = %d \r\n"),pDeviceID->dwPresetIDOffset));
	RETAILMSG (1, (TEXT("Get Device ID dwPlatformIDOffset = %d \r\n"),pDeviceID->dwPlatformIDOffset));
	RETAILMSG (1, (TEXT("Get Device ID dwPresetIDBytes = %d \r\n"),dwPresetIDBytes));
	RETAILMSG (1, (TEXT("Get Device ID dwPlatformIDBytes = %d \r\n"),dwPlatformIDBytes));
	
	memset(tPlatformIDOffset,0,sizeof(TCHAR)*100);

 if (GetPlatFormVersion() != 6 )
 {
	 wcscat(tPlatformIDOffset,pPresetIDOffset);
	//atow((LPSTR)pPresetIDOffset,tPlatformIDOffset,dwPresetIDBytes*2);
	RETAILMSG (1, (TEXT("Get Device ID PresetID = %s \r\n"),tPlatformIDOffset));
	wcscat(listBuffer,tPlatformIDOffset);
	memset(tPlatformIDOffset,0,sizeof(TCHAR)*100);
	memcpy(tPlatformIDOffset,pPlatformIDOffset,dwPlatformIDBytes);
	RETAILMSG (1, (TEXT("Get Device ID PlatformID= %s \r\n"),tPlatformIDOffset));
 }else
 {
	//atow(pPresetIDOffset,tPlatformIDOffset,dwPresetIDBytes*2);
	//wcscat(pPresetIDOffset,tPlatformIDOffset);
	RETAILMSG (1, (TEXT("Get Device ID PresetID = %s \r\n"),pPresetIDOffset));
	wcscat(listBuffer,pPresetIDOffset);

	LPTSTR pPlatformIDOffset2 = (LPTSTR)(Str + pDeviceID->dwPlatformIDOffset);

	memset(tPlatformIDOffset,0,sizeof(TCHAR)*100);
	//memcpy(tPlatformIDOffset,pPlatformIDOffset,dwPlatformIDBytes);
	atow((LPSTR)pPlatformIDOffset2 ,tPlatformIDOffset,dwPlatformIDBytes*2);
	RETAILMSG (1, (TEXT("Get Device ID PlatformID= %s \r\n"),tPlatformIDOffset));
 }	

	TCHAR pvParam[255]={0};
	if (!SystemParametersInfo( SPI_GETOEMINFO, 512,  (PVOID)& pvParam, 0))
	{
		DWORD dwerr = GetLastError();
		RETAILMSG (1, (TEXT("Get SystemParametersInfo OemName ERROR = %d \r\n"),dwerr));
	}else
		RETAILMSG (1, (TEXT("Get SystemParametersInfo  OemName= %s \r\n"),pvParam));

	//wsprintf(data,L"OEM Name:%d\r\n",GetPlatFormVersion());
	wcscat(szTmp,L"OEM Name:");
	wcscat(szTmp,pvParam);
	wcscat(szTmp,L"\r\n");
	
	memset(pvParam,0,512);
	if (!SystemParametersInfo( SPI_GETPLATFORMTYPE, 512,  (PVOID)& pvParam, 0))
	{
		DWORD dwerr = GetLastError();
		RETAILMSG (1, (TEXT("Get SystemParametersInfo Platform Type ERROR = %d \r\n"),dwerr));
	}else
		RETAILMSG (1, (TEXT("Get SystemParametersInfo Platform Type = %s \r\n"),pvParam));
 
	wcscat(szTmp,L"Platform Type:");
	wcscat(szTmp,pvParam);
	wcscat(szTmp,L"\r\n\r\n");
	wsprintf(data,L"Wince Version:%d\r\n",GetPlatFormVersion());
	wcscat(szTmp,data);
	MessageBox(NULL,szTmp,L"www.chinatmc.com.cn:",MB_YESNOCANCEL);

	
}
int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPTSTR    lpCmdLine,
					int       nCmdShow)
{
 	// TODO: Place code here.
	GetSystemInfo();
	return 0;
}

